/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.server;

import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
import org.internna.iwebjtracker.server.connection.ServerConnection;
import org.internna.iwebjtracker.server.connection.ConnectionDetails;

import static org.internna.iwebmvc.utils.StringUtils.hasText;

/**
 * Convenience implementation of the common protocol handling functionality.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public abstract class AbstractProtocolManagerServer<T extends ServerConnection, C extends ConnectionDetails> implements Server<T, C> {

    protected static final String HTTP_PROTOCOL = "http";
    protected static final String HTTPS_PROTOCOL = "https";

    private final Set<String> defaultProtocols;

    /**
     * Inits the default protocols.
     */
    public AbstractProtocolManagerServer() {
        defaultProtocols = new HashSet<String>(2);
        setSupportDefaultProtocols(true);
    }

    /**
     * Sets or removes the default protocols (HTTP/s) from the lsit of supported ones.
     */
    protected final void setSupportDefaultProtocols(boolean supportDefaultProtocols) {
        if (supportDefaultProtocols) {
            addSupportedProtocol(HTTP_PROTOCOL);
            addSupportedProtocol(HTTPS_PROTOCOL);
        } else {
            defaultProtocols.remove(HTTP_PROTOCOL);
            defaultProtocols.remove(HTTPS_PROTOCOL);
        }
    }

    /**
     * Adds a new protocol to the collection of supported ones.
     *
     * @param protocol any String
     */
    protected final void addSupportedProtocol(String protocol) {
        if (hasText(protocol)) {
            defaultProtocols.add(protocol);
        }
    }

    /**
     * Obtains the collection of protocols available when choosing a server of this type.
     *
     * @return not null (unmodifiable) collection of protocols this sever can handle.
     */
    @Override public final Set getSupportedProtocols() {
        return Collections.unmodifiableSet(defaultProtocols);
    }

    /**
     * Checks for a valid protocol. Valid protocols are those included in the supported protocols collection
     * for this server type.
     *
     * @param protocol any string
     * @return true if the protocol is valid for this server type.
     */
    protected boolean validateProtocol(String protocol) {
        return hasText(protocol) && getSupportedProtocols().contains(protocol);
    }

}
